public class NaturalSpline {
  
    ArrayList<PVector> points;
    ArrayList<Float> h;
    float [][] A;
    float [] r;
    ArrayList<Polynomial> polynomials;
    int SIZE;
    
    NaturalSpline(ArrayList<PVector> points) {
      this.points = points;
      this.SIZE = points.size();
      h = new ArrayList<Float>(); 
    } 
    
    
    public void run() {
       //Toca hacer aca el natural Spline
       fillH();
       fillAandR();
    }
    
    //Fill the h array that is the difference between xi+1 and xi
    private void fillH() {
        for(int i = 0; i < SIZE - 1; i++) {
            float xNext = points.get(i+1).x;
            float xCurrent = points.get(i).x;
            float hi = xNext - xCurrent;
            h.add(hi);
        }
    }
    
    //Fill the Matrix A for natural Spline
    private void fillAandR() {
          //Initial Matrix A and vector r
          A = new float[SIZE][SIZE];
          for(int i = 0; i < SIZE; i++) {
            r[i] = 0;
            for(int j = 0; j < SIZE; j++) {
                A[i][j] = 0;
            }
          }
          
          A[0][0] = 1;
          A[SIZE - 1][SIZE - 1] = 1;
          
          for(int i = 1 ; i < SIZE - 1 ; i++) {
             float a = 2*(h.get(i-1) + h.get(i));
             A[i][i] = a;
             A[i][i-1] = h.get(i-1);
             A[i][i+1] =  h.get(i);
          }
          /*
          for(int i = 0; i < SIZE; i++) {
            for(int j = 0; j < SIZE; j++) {
                println(i +","+j+"->"+A[i][j]);
            }
          }*/
    }
}

